//
//  HEOrderModel.m
//  HealthFamily
//
//  Created by 吴欧 on 2020/10/28.
//  Copyright © 2020 honglutec. All rights reserved.
//

#import "HEOrderModel.h"

@implementation HEOrderModel

+ (NSDictionary *)mj_objectClassInArray{
    return @{@"orderItemList" : [HEOrderItemModel class],
             @"childInfo" : [HEOrderChildInfo class]
    };
}


- (NSString *)KstatusName {
    if (!_KstatusName) {
        if (_channel == HEChannelTypeHomeCare && _status != HEOrderStatusWaitPay) {
            _KstatusName = _welfareStatusName;
        }else{
            _KstatusName = _statusName;
        }
    }
    return _KstatusName;
}

- (NSString *)KtotalAmount {
    if (!_KtotalAmount) {
        if (_visit) {
            _KtotalAmount = [NSString stringWithFormat:@"合计 ￥%@（含交通费￥%@）"
                             , [NSDecimalNumber rounding:self.payAmount afterPoint:2]
                             , [NSDecimalNumber rounding:self.freightAmount afterPoint:2]];
        }else {
            _KtotalAmount = [NSString stringWithFormat:@"合计 ￥%@（含运费￥%@）"
                             , [NSDecimalNumber rounding:self.payAmount afterPoint:2]
                             , [NSDecimalNumber rounding:self.freightAmount afterPoint:2]];
        }
    }
    return _KtotalAmount;
}

- (NSMutableAttributedString *)KFooterAmount {
    if (!_KFooterAmount) {
        NSString * total = @"";
        if (_visit) {
            total = [NSString stringWithFormat:@"（含交通费￥%@）￥%@",[NSDecimalNumber rounding:self.freightAmount afterPoint:2], [NSDecimalNumber rounding:self.payAmount afterPoint:2]];

        }else {
            total = [NSString stringWithFormat:@"（含运费￥%@）￥%@",[NSDecimalNumber rounding:self.freightAmount afterPoint:2], [NSDecimalNumber rounding:self.payAmount afterPoint:2]];
        }
        
        NSDictionary *attributes = @{ NSFontAttributeName:DYFont(16), NSForegroundColorAttributeName: [UIColor blackColor] };

        NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString: total attributes:attributes];
        NSRange loc = [[text string] rangeOfString:@"）"];
        
        NSInteger start = loc.location + 1;
        NSInteger len = total.length - loc.location - 1;
        
        //设置高亮色和点击事件
        [text yy_setTextHighlightRange:NSMakeRange(start, len) color:CPriceColor backgroundColor:[UIColor clearColor] tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
        }];
        
        // 右对齐
        NSMutableParagraphStyle *timeParagraphStyle = [[NSMutableParagraphStyle alloc] init];
        timeParagraphStyle.alignment = NSTextAlignmentRight;
        timeParagraphStyle.lineBreakMode = NSLineBreakByCharWrapping;
        [text addAttribute:NSParagraphStyleAttributeName value:timeParagraphStyle range:NSMakeRange(0, text.length)];
        [text addAttribute:NSFontAttributeName value:DYBlodFont(16) range:NSMakeRange(start, len)];

        _KFooterAmount = text;
    }
    return _KFooterAmount;
}

- (HEExtraDoctorInfoModel *)KdoctorInfo {
    if (!_KdoctorInfo) {
        NSDictionary *dict = [DYTools getContainersObjectFromJsonString:_extras];
        _KdoctorInfo = [HEExtraDoctorInfoModel mj_objectWithKeyValues:dict];
    }
    return _KdoctorInfo;
}

- (UIColor *)KstatusColor {
    if (!_KstatusColor) {
        if (_channel == HEChannelTypeHomeCare && _status != HEOrderStatusWaitPay) {
            switch (_welfareStatus) {
                case HEOrderWelfareStatusFinish:
                    _KstatusColor = CGrayColor;
                    break;
                case HEOrderWelfareStatusAutoRefurn:
                    _KstatusColor = CPriceColor;
                    break;
                default:
                    _KstatusColor = CNavBgColor;
                    break;
            }
        }else {
            switch (_status) {
                case HEOrderStatusWaitPay:
                case HEOrderStatusAfterSaleOrder:
                    _KstatusColor = CPriceColor;
                    break;
                case HEOrderStatusClosed:
                    _KstatusColor = CGrayColor;
                    break;
                default:
                    _KstatusColor = CNavBgColor;
                    break;
            }
        }
    }
    return _KstatusColor;
}

- (BOOL)isProxyPerson {
    UserInfo *userInfo = [UserManager sharedUserManager].curUserInfo;
    // 判断是不是发起代付人
    BOOL isAskPay = NO;
    if (self.KdoctorInfo.pay_agent_info != nil && [userInfo.id isEqualToString: self.KdoctorInfo.old_info.id]) {
        isAskPay = YES;
    }
    return !isAskPay;
}

- (HETranspFeesModel *)KtransFeesInfo {
    if (!_KtransFeesInfo) {
        NSDictionary *dict = [DYTools getContainersObjectFromJsonString: _transFeesInfo];
        _KtransFeesInfo = [HETranspFeesModel mj_objectWithKeyValues:dict];
    }
    return _KtransFeesInfo;
}

@end
